In [1]:
%pylab inline
from classy import *
from pylab import colorbar
In [2]:
d={}
for i in range(10):
d['%d' % i]='data/simple_digits/%d.png' % i
images=image.load_images_from_filepatterns(**d)
images.target_names=[int(_) for _ in images.target_names]
In [3]:
images.data[0].shape
Out[3]:
In [4]:
data=image.images_to_vectors(images)
In [5]:
image.vector_to_image(data.vectors[4,:],(16,12))
colorbar()
Out[5]:
In [6]:
data.vectors-=data.vectors.mean()
data.vectors/=data.vectors.std()
image.vector_to_image(data.vectors[4,:],(16,12))
colorbar()
Out[6]:
In [7]:
C=Perceptron()
In [8]:
timeit(reset=True)
C.fit(data.vectors,data.targets)
print("Training time: ",timeit())
In [9]:
print("On Training Set:",C.percent_correct(data.vectors,data.targets))
In [10]:
C.weights
Out[10]:
In [11]:
C.weights.shape
Out[11]:
In [12]:
C.output(data.vectors[2,:].reshape(1,-1))
Out[12]:
In [13]:
import matplotlib.pyplot as plt
plt.figure(figsize=(16,4))
for i in range(10):
plt.subplot(2,10,i+1)
image.vector_to_image(data.vectors[i,:],(16,12))
plt.axis('off')
plt.subplot(2,10,i+11)
image.vector_to_image(C.weights[i,:],(16,12))
plt.axis('off')
In [14]:
data.targets
Out[14]:
In [15]:
data.vectors.shape
Out[15]:
In [16]:
plt.plot(C.output(data.vectors[2,:]).ravel(),'-o')
In [17]:
C.output(data.vectors[2,:].reshape(1,-1)).shape
Out[17]:
In [ ]:
C=BackProp(hidden_layer_sizes = [4])
C.max_iter=5000
timeit(reset=True)
C.fit(data.vectors,data.targets)
print("Training time: ",timeit())
In [ ]:
print("On Training Set:",C.percent_correct(data.vectors,data.targets))
In [ ]:
C.layers_coef_[0].shape,C.layers_coef_[1].shape
In [ ]:
C.output(data.vectors[1,:].reshape(1,-1))
In [ ]: